#include<iostream>
#include<string>
#include<vector>
#include<boost/filesystem.hpp>
#include"util.hpp"

const std::string src_path = "data/input";
const std::string dst_path = "data/output/out.txt";

typedef struct DocInfo{
    std::string title;
    std::string content;
    std::string url;
}DocInfo_t;
//const & 输入形参数
//*：输出形参数
//&：输入输出形参数
bool EnumFile(const std::string &src_path,std::vector<std::string> *files_list);
bool ParseHtml(const std::vector<std::string> &files_list,std::vector<DocInfo_t> *results);
bool SaveHtml(const std::vector<DocInfo_t> &results,const std::string &dst_path);
int main()
{
    std::vector<std::string> files_list;
    std::vector<DocInfo_t> results;
    //首先递归式的将所有html文件路径保存至files_list/中，方便遍历读取。
    if(!EnumFile(src_path,&files_list))
    {
        std::cerr<<"enum file name error"<<std::endl;
        return 1;
    }
    //对files_list中的每个文件进行解析,解析结果保存至results中。
    if(!ParseHtml(files_list,&results))
    {
        std::cerr<<"parse html error"<<std::endl;
        return 2;
    }
    //将解析完的内容写入到out.txt中，以/3作为分割符。
    if(!SaveHtml(results,dst_path))
    {
        std::cerr<<"save html error"<<std::endl;
        return 3;
    }
    return 0;
}

bool EnumFile(const std::string &src_path,std::vector<std::string> *files_list)
{
    namespace fs=boost::filesystem;
    fs::path root_path(src_path);
    //用于判断root_path是否存在
    if(!fs::exists(root_path))
    {
        std::cerr<<src_path<<"no exists"<<std::endl;
        return false;
    }

    //定义一个空迭代器,用于判断迭代器是否结束
    fs::recursive_directory_iterator end;
    for(fs::recursive_directory_iterator iter(root_path);iter!=end;iter++)
    {   //用于判断是不是常规文件，过滤掉文件夹路径
        if(!fs::is_regular_file(*iter))
        {
            continue;
        }
        //用于判断是不是.html文件
        if(iter->path().extension()!=".html")
        {
            continue;
        }
        //path中的string方法可以将boost库路径转换成C++的string路径
        // std::cout<<"debug:"<<iter->path().string()<<std::endl;
        files_list->push_back(iter->path().string());
    }
    return true;
}

static bool ParseTitle(const std::string &file,std::string *title)
{
    std::size_t begin = file.find("<title>");
    if(begin==std::string::npos)
    {
        return false;
    }
    std::size_t end = file.find("</title>");
    if(end==std::string::npos)
    {
        return false;
    }
    begin+=std::string("<title>").size();
    if(begin>end)
    {
        return false;
    }
    *title =file.substr(begin,end-begin);
    return true;

}

static bool ParseContent(const std::string &file,std::string *content)
{
    enum status
    {
        LABLE,
        CONTENT
    };
    enum status s = LABLE;
    for(auto c: file)
    {
        switch (s)
        {
        case LABLE:
            if(c=='>') s = CONTENT;
            break;
        case CONTENT:
            if(c=='<') s = LABLE;
            else{
                if(c=='\n')
                {
                    c = ' ';
                }//将文档中的\n替换成空格，充当文档内部的分隔符
                content->push_back(c);
            }
            break;        
        default:
            break;
        }
    }
    return true;
}
static bool ParseUrl(const std::string file_path,std::string *url)
{
    std::string url_head = "https://www.boost.org/doc/libs/1_85_0/doc/html";
    std::string url_tail = file_path.substr(src_path.size());
    *url = url_head+url_tail;
    return true;
}
static void ShowDoc(const DocInfo_t &doc)
{
    std::cout<<"title: "<<doc.title<<std::endl;
    std::cout<<"content: "<<doc.content<<std::endl;
    std::cout<<"url: "<<doc.url<<std::endl;
}
bool ParseHtml(const std::vector<std::string> &files_list,std::vector<DocInfo_t> *results)
{
    for(auto file:files_list)
    {
        std::string result;
        if(!ns_util::FileUtil::ReadFile(file,&result))
        {
            continue;
        }
        DocInfo_t doc;
        if(!ParseTitle(result,&doc.title))
        {
            continue;
        }
        if(!ParseContent(result,&doc.content))
        {
            continue;
        }
        if(!ParseUrl(file,&doc.url))
        {
            continue;
        }
        results->push_back(std::move(doc));
        //for debug
        // ShowDoc(doc);
        // break;
    }
    return true;
}
bool SaveHtml(const std::vector<DocInfo_t> &results,const std::string &output)
{
    #define SEP '\3'
    std::ofstream out(output,std::ios::out|std::ios::binary);
    if(!out.is_open())
    {
        std::cerr<<"open "<<output<<" failed!"<<std::endl;
        return false;
    }
    // std::cout<<results.size()<<std::endl;
    for(auto &result:results)
    {
        std::string out_string;
        out_string = result.title;
        out_string += SEP;
        out_string += result.content;
        out_string += SEP;
        out_string += result.url;
        out_string += '\n';
        // std::cout<<out_string<<std::endl;
        out.write(out_string.c_str(),out_string.size());
        // break;
    }
    out.close();
    return true;
}